/**

 * Copyright (c) subnew
 * Licensed under the GPL;
 * @email:joseanluo@gmail.com
 * @Title: LoginController.java
 * @Package com.subnew.copyman.modules.sys.controller.login
 */
package com.subnew.mango.modules.sys.controller.login;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.subnew.mango.frame.constant.Globals;
import com.subnew.mango.frame.dao.EntityManager;
import com.subnew.mango.frame.exception.ActionException;
import com.subnew.mango.frame.model.common.AjaxResponse;
import com.subnew.mango.frame.model.common.SessionInfo;
import com.subnew.mango.frame.utils.SecurityUtils;
import com.subnew.mango.frame.utils.StringUtil;
import com.subnew.mango.frame.utils.web.SpringmvcContextHolderUtil;
import com.subnew.mango.frame.web.controller.BaseController;
import com.subnew.mango.modules.sys.entity.SysOperator;
import com.subnew.mango.modules.sys.service.operator.OperatorManager;


/**
 * 用户登录/注销等前端交互入口
 * @author JoseanLuo
 * @date 2014年9月2日 上午11:56:01
 * @version V1.0
 */
@Controller
@RequestMapping(value="login")
public class LoginController extends BaseController<SysOperator>{
	
	
	@Autowired
	private OperatorManager operatorManager;

	/* (non-Javadoc)
	 * @see com.subnew.copyman.frame.web.controller.BaseController#getEntityManager()
	 */
	@Override
	public EntityManager<SysOperator, Long> getEntityManager() {
		return null;
	}

	/**
	 * 
	* 请求进入登陆页面<br>
	* 可带参数主题，默认主题为'leting'
	* @return 返回登陆页面
	* @throws ActionException  
	* @Title: login
	* @author JoseanLuo  
	* @date 2014年12月15日 上午12:16:56
	* @version V1.0
	 */
	@RequestMapping(value={"login",""})
	public String login() throws  ActionException{
		//getSession().setAttribute("userName", "JoseanLuo");
		//getSession().setAttribute("roleName", "系统管理员");
		//return "workspace/main";
		String loginStyle="jw";
		String theme=(String) SpringmvcContextHolderUtil.getRequest().getAttribute("theme");
		if(!StringUtil.isBlank(theme)){
			loginStyle=theme;
		}
		SpringmvcContextHolderUtil.getRequest().getSession().setAttribute("theme", loginStyle);
		return "login/login-"+loginStyle;
	}
	
	/**
	 * 
	* 请求登陆，对用户密码、验证码进入校验
	* @param name 用户名
	* @param password 密码
	* @param verifyimage 验证码
	* @return 登陆是否成功AjaxResponse
	* @throws ActionException  
	* @Title: doLogin
	* @author JoseanLuo  
	* @date 2014年12月15日 上午12:18:32
	* @version V1.0
	 */
	@ResponseBody
	@RequestMapping(value="toLogin")
	public AjaxResponse toLogin(String loginName,String password,String verifyimage) throws ActionException{
		AjaxResponse response= new AjaxResponse();
		
		//用户名或密码为null 直接返回
		if(StringUtil.isBlank(loginName)||StringUtil.isBlank(password)){
			response.setCode("000001");
			response.setMessage("用户名/密码 不能为空！");
			return response;
		}
		//验证码为null,直接返回 
		if(StringUtil.isBlank(verifyimage)){
			response.setCode("000002");
			response.setMessage("验证码 不能为空！");
			return response;
		}
		//验证码输入出错 
		if(!verifyimage.equalsIgnoreCase((String)SpringmvcContextHolderUtil.getSession().getAttribute(Globals.SESSION_KEY_OF_RAND_CODE))){
			response.setCode("000003");
			response.setMessage("验证码 不正确！");
			return response;
		}
		//与数据库中数据校验
		SysOperator queryOperator= new SysOperator();
		queryOperator.setLoginname(loginName);
		queryOperator.setPwd(password);
        SysOperator operator=operatorManager.findUnique(queryOperator);
          //不存在用户
        if(null==operator){
        	response.setCode("000004");
			response.setMessage("用户名或密码错误！");
			return response;
        }
        SecurityUtils.putOperatorToSession(operator);
		return response;
	}
	
	@RequestMapping(value="wecome")
	public String wecome() throws ActionException{
		String theme=(String) SpringmvcContextHolderUtil.getSession().getAttribute("theme");
		if(StringUtil.isBlank(theme)){
			theme="leting";
		}
		SessionInfo info= (SessionInfo) SpringmvcContextHolderUtil.getSession().getAttribute(Globals.SESSION_KEY_OF_USER);
		//非法登陆
		if (null==info) {
           return  "redirect:/login";		
		}
		return "workspace/main";
	}
}
